//
// Created by Administrator on 2025/3/20.
//

#include "elf_game_full_load_controller.h"

static void ui_elf_game_full_load_controller_cb(lv_timer_t *timer){
    LOG_INFO_WRAPPER("start");
    elf_game_module_t *module = (elf_game_module_t *)timer->user_data;

    lv_timer_del(timer);
    module->screen->full_screen_load_timer = NULL;

    // 模仿请求数据过程
    bool request_result = true;
//    bool request_result = false;
    if(request_result) {
        // 精灵信息数据
        module->data->coin = 100;
        module->data->mood = 66;
        module->data->intimate = 88;
        // 道具列表
        if(module->data->prop_list){
            free(module->data->prop_list);
            module->data->prop_list = NULL;
            module->data->prop_count = 0;
        }
//        char *prop_list[] = {"糯米滋","披萨","烧鸡","毛绒球","蹦跳球","猫抓板","毛巾","香波","刷子"};
        prop_data_t prop_list[] = {
                {.name="糯米糍",.id=101,.coin=40,.count=1,.type=0,.mood_value=5},
                {.name="披萨",.id=102,.coin=35,.count=5,.type=0,.mood_value=10},
                {.name="烧鸡",.id=103,.coin=60,.count=5,.type=0,.mood_value=10},
                {.name="毛绒球",.id=104,.coin=70,.count=10,.type=1,.mood_value=5},
                {.name="蹦跳球",.id=105,.coin=80,.count=5,.type=1,.mood_value=5},
                {.name="猫抓板",.id=106,.coin=90,.count=5,.type=1,.mood_value=10},
                {.name="毛巾",.id=107,.coin=100,.count=6,.type=2,.mood_value=10},
                {.name="香波",.id=108,.coin=110,.count=7,.type=2,.mood_value=5},
                {.name="刷子",.id=109,.coin=120,.count=8,.type=2,.mood_value=5},
        };
        int prop_num = sizeof(prop_list)/sizeof(prop_list[0]);
        module->data->prop_list = malloc_set_zero(sizeof(prop_data_t) * prop_num);
        module->data->prop_count = prop_num;
        for (int i = 0; i < prop_num; ++i) {

            module->data->prop_list[i].coin = prop_list[i].coin;
            strncpy(module->data->prop_list[i].name, prop_list[i].name, sizeof(module->data->prop_list[i].name)-1);
            module->data->prop_list[i].id = prop_list[i].id;
            module->data->prop_list[i].type = prop_list[i].type;
            module->data->prop_list[i].count = prop_list[i].count;
            module->data->prop_list[i].mood_value = prop_list[i].mood_value;
        }
        // 显示宠物信息页面
        if(module->screen->current_page == ELF_GAME_FULL_LOAD_SCREEN){
//            ui_elf_game_full_load_screen_deinit(module);
            module->screen->deinit_cb();
            ui_elf_game_elf_info_screen_show(module);
        }
    }else{
        // 显示网络错误页面
        if(module->screen->current_page == ELF_GAME_FULL_LOAD_SCREEN){
//            ui_elf_game_full_load_screen_deinit(module);
            module->screen->deinit_cb();
            ui_elf_not_connect_net_screen_show(module);
        }
    }
}

void elf_gmae_full_load_controller(){
    /**
        * 定時器模范加载过程
        *  1.获取数据成功，显示宠物信息页面
        *  2.网络断了或者获取数据失败，显示断网界面
        *  3.跳转前判断是否在加载页面，在加载页面才跳转
        *
        *  按键：返回键，退出当前页面，关闭定时器(请求后不处理，即不跳转页面)
        */
    if(elf_game_module.screen->full_screen_load_timer){
        lv_timer_del(elf_game_module.screen->full_screen_load_timer);
        elf_game_module.screen->full_screen_load_timer = NULL;
    }
    elf_game_module.screen->full_screen_load_timer = lv_timer_create(ui_elf_game_full_load_controller_cb,  1000, &elf_game_module);
}


